package com.project.springboot.component;

import com.project.springboot.dataobject.Product;
import com.project.springboot.repository.ProductRepository;
import com.project.springboot.utils.MapUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description：数据初始化类
 * Created by 朱正磊 on 2019-04-18 14:38
 */
@Component
public class MyInit {

    //初始化的全局静态变量
    public static Map<String, Object> cache = null;

    @Autowired
    private ProductRepository productRepositoryTemp;

    private static ProductRepository productRepository;

    /**
     * description：初始化数据
     * user 朱正磊
     * time 2019-04-18 15:10
     */
    @PostConstruct //字面上看, 这个注解的意思是在构造方法之后执行(还有个PreConstruct方法)
    public void init() { //程序启动后会自动执行该方法
        try {
            productRepository = productRepositoryTemp; //将注入的对象重新赋值给静态对象
            Product product = productRepository.findByPid(1); //在这里就可以调用Service的方法了
            cache = MapUtil.javaBean2Map(product);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Product method() {
        return productRepository.findByPid(2);
    }

}
